package com.sls.web.manager.game.service.impl;

import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.Supply;
import com.sls.web.manager.game.querybean.SupplyQuery;
import com.sls.web.manager.game.service.SupplyManagerService;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

@Repository("supplyManagerService")
public class SupplyManagerServiceImpl extends SuperService implements
		SupplyManagerService {
	private static Logger log = Logger
			.getLogger(SupplyManagerServiceImpl.class);
	private Gson gson;

	public SupplyManagerServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public boolean add(Supply supply, UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter SupplyManagerService add method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.SUPPLY_INFO_ADD);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = new SocketDTO();
		dto = super.send(head, supply);
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Leave SupplyManagerService add method...");
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public Pagination<Supply> querySupply(SupplyQuery supplyQuery,
			UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter SupplyManagerService querySupply method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.SUPPLY_INFO_PAGE_QUERY);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, supplyQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<Supply> supply = gson.fromJson(body,
					new TypeToken<Pagination<Supply>>() {
					}.getType());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave PSupplyManagerService querySupply method...");
			return supply;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public boolean update(Supply supply, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter SupplyManagerService update method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.SUPPLY_INFO_UPDATE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, supply);
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Leave SupplyManagerService update method...");
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Supply querySupplyById(String id, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter SupplyManagerService querySupplyById method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.SUPPLY_INFO_QUERY_BY_ID);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		Supply supply = new Supply();
		supply.setSupplyId(id);
		SocketDTO dto = super.send(head, supply);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			Supply supply2 = gson.fromJson(dto.getBody(), Supply.class);
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave SupplyManagerService querySupply method...");
			return supply2;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public List<Supply> queryAllSupply(String supplyState, UserInfoVO userInfoVO)
			throws Exception {
		SupplyQuery query = new SupplyQuery();
		query.setCurrentPage(1);
		query.setPageSize(20);
		query.setSupplyState(supplyState);

		List<Supply> supplyList = new LinkedList<Supply>();
		Pagination<Supply> page = querySupply(query, userInfoVO);
		if (page != null) {
			if (page.getList() != null) {
				supplyList.addAll(page.getList());
			}
			int count = page.getRowCount();
			if (query.getPageSize() == 0) {
				query.setPageSize(1);
			}
			for (int i = 1; i < Math.ceil(count * 1.0 / query.getPageSize()); i++) {
				query.setCurrentPage(i + 1);
				page = querySupply(query, userInfoVO);
				if (page.getList() != null) {
					supplyList.addAll(page.getList());
				}
			}
			return supplyList;
		}
		return null;
	}

}
